from django.shortcuts import render

# Create your views here.

from django.views.generic import View
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView
from rest_framework.views import APIView

from goods.models import GoodsCategory,GoodsChannel, SKU
from contents.models import ContentCategory
from collections import OrderedDict

# Create your views here.
from goods.serializers import HotSKUSerializer
from utils.pagination import StandardResultsSetPagination


class CategoryView(View):
    """
    获取首页分类数据

    GET /goods/categories/
    """
    def get(self,request):
        pass


"""
获取商品列表 推荐信息


# 1.前段应该传递过来一个分类id,我们应该接收这个分类id
# 2.根据分类id查询商品数据,并对商品数据进行排序,并获取2个  [SKU,SKU,SKU]
# 3.我们需要对数据进行  序列化操作(将对象转换为字典)
# 4.返回数据

GET     goods/categories/cat_id/hotskus/



"""
#
from rest_framework.response import Response


# class HotSKUView(APIView):
#     def get(self,request,category_id):
#         # 1.前段应该传递过来一个分类id,我们应该接收这个分类id
#
#         # 2.根据分类id查询商品数据,并对商品数据进行排序,并获取2个  [SKU,SKU,SKU]
#         skus=SKU.objects.filter(category_id=category_id,is_launched=True).order_by('-sales')[:2]
#         # 3.我们需要对数据进行  序列化操作(将对象转换为字典)
#         serializer=HotSKUSerializer(skus,many=True)
#
#         # 4.返回数据
#         return Response(serializer.data)

class HotSKUView(ListAPIView):

    pagination_class = None

    serializer_class = HotSKUSerializer


    def get_queryset(self):

        category_id = self.kwargs['category_id']
        return SKU.objects.filter(category_id=category_id,is_launched=True).order_by('-sales')[:2]





"""
列表页面数据获取


1. 先实现返回所有分类数据
2. 再实现排序
3. 最后实现 分页

# 请求方式 GET /goods/categories/(?P<category_id>\d+)/skus/?page=xxx&page_size=xxx&ordering=xxx


GET     /goods/categories/(?P<category_id>\d+)/skus/?ordering=-price&page_size=2&page=3
"""

# from rest_framework.generics import GenericAPIView
class SKUListAPIView(ListAPIView):
    # # 排序
    filter_backends = [OrderingFilter]
    # # 设置排序字段
    ordering_fields=['create_time','price','sales']

    #分页类
    # pagination_class = StandardResultsSetPagination

    # 127.0.0.1:8000/books/?ordering=-readcount
    serializer_class = HotSKUSerializer

    def get_queryset(self):
        category_id = self.kwargs['category_id']

        return SKU.objects.filter(category_id=category_id, is_launched=True)

"""

搜索的原理: 对数据进行分词处理, 把分词的数据进行一个 记录

举例:
    我是中国人
    我           第一条记录
    中国          第5条记录
    中国人         第10条记录


我们实现搜索的是的步骤:
一: 安装elasticsearch

二: 让 haystack 帮我们区实现搜索功能
    ① 配置信息
    ② 生成索引
    ③ 搜索


"""
from .serializers import SKUIndexSerializer
from drf_haystack.viewsets import HaystackViewSet

class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer





